from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from goodsapp.models import *
from goodsapp.sers import *


class IndexView(APIView):
    def get(self,request):
        queryset=Cate.objects.all()
        ser=CateSer(queryset,many=True)
        data_list=[]
        for item in queryset:
            data={}
            data['cate']=CateSer(item).data    # 查找到 分类下的所有商品
            goods_queryset=Goods.objects.filter(cate_id=item.id)                #通过商品外键id等于分类id
            data['good_list']=GoodsSer(goods_queryset,many=True).data           #将商品信息赋值到data字典里
            data_list.append(data)                  #然后将分类和商品赋值的信息合并到列表里进行展示
        return Response(data_list)
